package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.bocai.yuweiwangluo.activity.HomeActivity;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.LoginBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.ILoginView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class LoginPresenter
{
    private LoginBean loginBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private ILoginView loginView;
    private Context context;
    private BaseView baseView;
    public LoginPresenter(Context context, ILoginView loginView,BaseView baseView)
    {
        this.context = context;
        this.loginView = loginView;
        this.baseView=baseView;
    }

    /**
     * 用户登录请求
     */
    public void login()
    {
        if("".equals(loginView.getUserName()))
        {
            Toast.makeText(context,"请输入手机号", Toast.LENGTH_SHORT).show();
        }
        else if (loginView.getUserName().length()!= 11)
        {
            Toast.makeText(context, "手机号码输入不正确", Toast.LENGTH_LONG).show();
        } else if ("".equals(loginView.getPassWord()))
        {
            Toast.makeText(context, "密码不能为空", Toast.LENGTH_LONG).show();
        } else
        {
            baseView.showLoading();
            MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
            myOkHttpClient.login(UrlData.BASEURL + UrlData.LOGIN, loginView.getUserName(),loginView. getPassWord(), handler);
        }
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        baseView.hideLoading();
                        Log.e("loginresult", result);
                        loginBean= gson.fromJson(result, LoginBean.class);
                        if (0==loginBean.getState())
                        {
                            loginView.callBack(loginBean.getData().getId());
                            MyConst.user_id=loginBean.getData().getId();
                            SharedPreferences preferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
                            SharedPreferences.Editor edit =preferences.edit();
                            edit.putString("user_name",loginView.getUserName());
                            edit.putString("password",loginView.getPassWord());
                            edit.putString("user_id",loginBean.getData().getId());
                            edit.putString("nickname",loginBean.getData().getNickname());
                            edit.putString("flag",loginBean.getData().getFlag());
                            edit.putString("state",loginBean.getData().getState());
                            edit.putString("number",loginBean.getData().getNumber());
                            edit.putString("isremember",loginView.getRemeberPsd()+"");
                            edit.commit();
                            context.startActivity(new Intent(context, HomeActivity.class));
                            loginView.finishActivity();
                        } else
                        {
                            Toast.makeText(context, loginBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
}